import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:syria_sports/core/app_export.dart';
import 'package:syria_sports/data/controllers/exercises_controller.dart';
import 'package:syria_sports/presentation/tools/exercise_tools_page/bloc/exercise_tools_screen.dart';
import 'package:syria_sports/presentation/exercises/exercises_page/models/exercise.dart';

import '../../../../dialogs/delete_player_confirmation.dart';
import '../../add_exercise_page/bloc/add_exercise_page_screen.dart';
import '../../../coach_notes_page/bloc/coach_notes_screen.dart';

class ExerciseItemView extends StatelessWidget {
  final Exercise exercise;
  const ExerciseItemView({super.key, required this.exercise});

  @override
  Widget build(BuildContext context) {

    return Slidable(
      enabled: PrefUtils.isGymAdmin(),
      // The start action pane is the one at the left or the top side.
      startActionPane: ActionPane(
        // A motion is a widget used to control how the pane animates.
        motion: const ScrollMotion(),

        /*// A pane can dismiss the Slidable.
        dismissible: DismissiblePane(onDismissed: () {}),*/

        // All actions are defined in the children parameter.
        children: [
          // A SlidableAction can have an icon and/or a label.
          SlidableAction(
            onPressed: (context){
              showDeleteConfirmation(context, exercise);
            },
            backgroundColor: Colors.transparent,
            foregroundColor: Colors.red,
            icon: Icons.delete,
            label: 'حذف',
          ),
          SlidableAction(
            onPressed: (context){
              Navigator.push(context, MaterialPageRoute(builder: (context) => AddExercisePageScreen(exercise:exercise,)));
            },
            backgroundColor: Colors.transparent,
            foregroundColor: Colors.orange,
            icon: Icons.edit,
            label: 'تعديل',
          ),
        ],
      ),
      child: InkWell(
        onTap: (){
          if(PrefUtils.isCoach()) {
            Navigator.push(context, MaterialPageRoute(builder: (context) =>  CoachNotesScreen(exercise: exercise,)));
          }
        },
        child: Card(
          color: Theme.of(context).colorScheme.secondary,
          child: Padding(
            padding: const EdgeInsets.all(8.0),
            child: Row(
              children: [
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(exercise.name,style: theme.textTheme.titleMedium),
                      Row(
                        children: [
                          Icon(Icons.group,color: Colors.red,),
                          SizedBox(width: 4),
                          Text(exercise.targetGroup,style: CustomTextStyles.bodySmallOnPrimary),
                        ],
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          exercise.description,
                          overflow: TextOverflow.ellipsis,
                          style: theme.textTheme.labelMedium,
                          maxLines: 50,
                        ),
                      ),
                      Row(
                        children: [
                          Text('يبدأ:  ${exercise.fromTime}', style: theme.textTheme.displaySmall,),
                          SizedBox(width: 4),
                          Text('ينتهي: ${exercise.toTime}', style: theme.textTheme.displaySmall,),
                        ],
                      ),
                    ],
                  ),
                ),
                Column(
                  children: [
                    IconButton(
                        onPressed: (){
                      if(PrefUtils.isCoach()) {
                        Navigator.push(context, MaterialPageRoute(builder: (context) =>  CoachNotesScreen(exercise: exercise,)));
                      }
                    }, icon: Icon(Icons.notes,color: theme.textTheme.labelMedium?.color,)),
                    IconButton(onPressed: (){
                      if(PrefUtils.isCoach()) {
                        Navigator.push(context, MaterialPageRoute(builder: (context) =>  ExerciseToolsScreen.builder(context: context,exercise: exercise,)));
                      }
                    }, icon: Icon(Icons.more),color: theme.textTheme.labelMedium?.color),
                  ],
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
  void showDeleteConfirmation(BuildContext context, Exercise exercise) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return DeleteConfirmationDialog(
          playerName: exercise.name,
          titleUser: 'التمرين',
          onConfirm: () {
            ExercisesController.deleteById(exercise.exerciseId??'-1');
            if (kDebugMode) {
              print('تم حذف تمرين ${exercise.name}!');
            }
          },
        );
      },
    );
  }
}
